package com.burger.counter.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.burger.counter.Constants.SQLConstants;
import com.burger.counter.beans.User;
import com.burger.counter.util.DBConnection;

public class UserDao {
	
	public void insertUserDetails(String firstName, String lastName, String eMail, int zipcode, String dateOfBirth, int regionID, String password, String isAdminOrCustomer){
        Connection con=null;
        PreparedStatement prest =null;
        try{
            con=DBConnection.getConnection();
            System.out.println("Creating DB Connection");
            String prepStatement=SQLConstants.insertUserDetails;
            prest = con.prepareStatement(prepStatement);
            
            prest.setString(1, firstName);
            prest.setString(2, lastName);
            prest.setString(3, eMail);
            prest.setInt(4, zipcode);
            prest.setString(5, dateOfBirth);
            prest.setInt(6, regionID);
            prest.setString(7, password);
            prest.setString(8, isAdminOrCustomer);
            
            prest.executeUpdate();
            
            System.out.println("A user got added");
        }
        catch(Exception e){
            
        }
	}    

	public User[] getUserDetails(String email){
		Connection con=null;
		ResultSet rs=null;
		PreparedStatement prest =null;
		User[] userList=null;
		int size;
		try{
			con=DBConnection.getConnection();
			System.out.println("Creating DB Connection");
			String prepStatement=SQLConstants.getUser;
			prest = con.prepareStatement(prepStatement);
			prest.setString(1, email);
			
			rs = prest.executeQuery();
			if (rs != null) {
				rs.last();
				size = rs.getRow();
				userList = new User[size];
				int currRow = 0;
				rs.beforeFirst();

				while (rs.next()) {
					User p = new User();
					p.setUserId(rs.getInt(1));
					p.setPassword(rs.getString(2));
					p.setRegionID(rs.getInt(3));
					p.setFirstName(rs.getString(4));
					p.setLastName(rs.getString(5));
					p.setZipcode(rs.getInt(6));
					p.setDateOfBirth(rs.getDate(7));
					userList[currRow] = p;
					currRow++;
				}
			}		

		}
		catch(Exception e){
			
		}
		
		return userList;
	}
	
	public void updateUserData(String firstName, String lastName, String eMail, int zipcode, String dateOfBirth, int regionID, String password, String isAdminOrCustomer,Integer userId){
        Connection con=null;
        PreparedStatement prest =null;
        try{
            con=DBConnection.getConnection();
            System.out.println("Creating DB Connection");
            String prepStatement=SQLConstants.updateUserDetails;
            prest = con.prepareStatement(prepStatement);
            
            prest.setString(1, firstName);
            prest.setString(2, lastName);
            prest.setInt(3, zipcode);
            prest.setString(4, dateOfBirth);
            prest.setInt(5, regionID);
            prest.setString(6, password);
            prest.setString(7, isAdminOrCustomer);
            prest.setString(8, eMail);
            prest.setInt(9, userId);
            
            prest.executeUpdate();
            
            System.out.println("A user got updated");
        }
        catch(Exception e){
            
        }
	}    
	
	public void setUserFirstName(String firstName, String email){
		Connection con=null;
		int rs;
		PreparedStatement prest =null;
		try{
			con=DBConnection.getConnection();
			System.out.println("Creating DB Connection");
			String prepStatement=SQLConstants.updateUserFirstName;
			prest = con.prepareStatement(prepStatement);
			prest.setString(1, firstName);
			prest.setString(2, email);
			
			rs = prest.executeUpdate();
			if (rs == 1) {
				System.out.println("User's First Name has been updated");
			}					

		}
		catch(Exception e){
			
		}		
	}
	
	public void setUserLastName(String lastName, String email){
		Connection con=null;
		int rs;
		PreparedStatement prest =null;
		try{
			con=DBConnection.getConnection();
			System.out.println("Creating DB Connection");
			String prepStatement=SQLConstants.updateUserLastName;
			prest = con.prepareStatement(prepStatement);
			prest.setString(1, lastName);
			prest.setString(2, email);
			
			rs = prest.executeUpdate();
			if (rs == 1) {
				System.out.println("User's Last Name has been updated");
			}					

		}
		catch(Exception e){
			
		}		
	}
	
	public void setUserZipcode(int zipcode, String email){
		Connection con=null;
		int rs;
		PreparedStatement prest =null;
		try{
			con=DBConnection.getConnection();
			System.out.println("Creating DB Connection");
			String prepStatement=SQLConstants.updateUserZipcode;
			prest = con.prepareStatement(prepStatement);
			prest.setInt(1, zipcode);
			prest.setString(2, email);
			
			rs = prest.executeUpdate();
			if (rs == 1) {
				System.out.println("User's Zipcode has been updated");
			}					

		}
		catch(Exception e){
			
		}		
	}
	
	public void setUserDateOfBirth(String dateOfBirth, String email){
		Connection con=null;
		int rs;
		PreparedStatement prest =null;
		try{
			con=DBConnection.getConnection();
			System.out.println("Creating DB Connection");
			String prepStatement=SQLConstants.updateUserDateOfBirth;
			prest = con.prepareStatement(prepStatement);
			prest.setString(1, dateOfBirth);
			prest.setString(2, email);
			
			rs = prest.executeUpdate();
			if (rs == 1) {
				System.out.println("User's date of birth has been updated");
			}					

		}
		catch(Exception e){
			
		}		
	}
	
	public void setUserRegionId(int regionId, String email){
		Connection con=null;
		int rs;
		PreparedStatement prest =null;
		try{
			con=DBConnection.getConnection();
			System.out.println("Creating DB Connection");
			String prepStatement=SQLConstants.updateUserRegionId;
			prest = con.prepareStatement(prepStatement);
			prest.setInt(1, regionId);
			prest.setString(2, email);
			
			rs = prest.executeUpdate();
			if (rs == 1) {
				System.out.println("User's Region Id has been updated");
			}					

		}
		catch(Exception e){
			
		}		
	}
	
	public void setUserPassword(String password, String email){
		Connection con=null;
		int rs;
		PreparedStatement prest =null;
		try{
			con=DBConnection.getConnection();
			System.out.println("Creating DB Connection");
			String prepStatement=SQLConstants.updateUserPassword;
			prest = con.prepareStatement(prepStatement);
			prest.setString(1, password);
			prest.setString(2, email);
			
			rs = prest.executeUpdate();
			if (rs == 1) {
				System.out.println("User's password has been updated");
			}					

		}
		catch(Exception e){
			
		}		
	}
}
